package com.jd.pet.service.impl;

import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.*;
import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.domain.SearchMasterMsgAuditLog;
import com.jd.pet.mapper.SearchMasterMsgAuditLogMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.pet.service.ISearchMasterMsgService;
import com.jd.user.domain.Logininfo;
import com.jd.user.domain.User;
import com.jd.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
@Transactional
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public AjaxResult publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //1.对象中的其他属性准备：
        //state状态【0待审核，1审核通过待处理，-1审核失败驳回，2处理完成，3寻主池】
        searchMasterMsg.setState(0);
        // 获取登陆人信息，
        Logininfo logininfo = LoginContext.getLoginfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        //user_id【哪一个用户发布的=当前登录人】，shop_id【分配给了哪一个店铺】
        searchMasterMsg.setUserId(user.getId());
        //2.添加到数据库
        searchMasterMsgMapper.add(searchMasterMsg);
        //3.百度AI智能审核
        // 标题和名字审核
        Boolean boo1 = BaiduAIUtils.TextCensor(searchMasterMsg.getTitle() + searchMasterMsg.getName());
        // 图片审核
        Boolean boo2 = BaiduAIUtils.ImgsCensor(searchMasterMsg.getResources());
        // 添加日志
        SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
        log.setMsgId(searchMasterMsg.getId()); // 设置消息id
        if (boo1 && boo2) { // 智能审核通过
            //3.1.审核【name，title，resources】通过：state=1，添加审核日志，发短信给用户告诉寻主消息审核通过
            searchMasterMsg.setState(1);
            // 将地址与店铺地址比较，发短信给最近的店铺设置shopId
            Point userPoint = DistanceUtils.getPoint(searchMasterMsg.getAddress());
            Shop nearestShop = DistanceUtils.getNearestShop(userPoint, shopMapper.findAll());
            if (nearestShop == null) { // 没有最近的，设置到寻主池
                searchMasterMsg.setState(3);
            } else {
                searchMasterMsg.setShopId(nearestShop.getId()); // 设置shopId
                // 给店铺管理员发短信通知订单
                Employee employee = employeeMapper.findOne(nearestShop.getAdminId());
                System.out.println("你的店铺有新的订单，请联系用户"
                        +user.getPhone());
            }
            searchMasterMsgMapper.update(searchMasterMsg);
            log.setState(1);
            log.setNote("审核通过");
            searchMasterMsgAuditLogMapper.add(log);
            // 发短信提醒用户审核通过
//            SmsUtils.sendSms(user.getPhone(), "您发布的寻主消息通过审核！");
            System.out.println("您发布的寻主消息通过审核！");
            return new AjaxResult();
        } else {
            //3.2.审核失败驳回：state=-1，添加审核日志，发短信给用户告诉寻主消息审核失败
            searchMasterMsg.setState(-1);
            log.setState(0); // 审核失败
            log.setNote("审核失败");
            searchMasterMsgAuditLogMapper.add(log);
            searchMasterMsgMapper.del(searchMasterMsg.getId());
            // 给用户发送审核失败
            System.out.println("您发布的寻主消息未通过审核！请重新发布！");
            return AjaxResult.me().setSuccess(false).setMessage("您发布的寻主消息未通过审核！请重新发布！");
        }
        //如果获取当前登录人？如何计算离当前用户最近的店铺？如何审核多张图片？
    }
}
